package com.hopu.blb_erp.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopu.blb_erp.entity.*;
import com.hopu.blb_erp.mapper.*;
import com.hopu.blb_erp.service.TIoWarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

@Service
    public class TIoWarehouseServiceImpl extends ServiceImpl<TIoWarehouseMapper,TIoWarehouse> implements TIoWarehouseService {
    @Autowired
    private TIoWarehouseMapper tIoWarehouseMapper;
    @Override
    public List<TIoWarehouse> findTIo(String wName) {
        return tIoWarehouseMapper.selectTIo(wName) ;

    }
    @Override
    public TIoWarehouse findTIos(String wName) {
        return tIoWarehouseMapper.selectTIos(wName) ;

    }
    @Autowired
    private TIoWarehouseMapper ioWarehouseMapper;
    @Autowired
    private CargoMapper cargoMapper;
    @Autowired
    private TMenuMapper tMenuMapper;
    @Autowired
    private TIomWarehouseMapper tIomWarehouseMapper;
    @Autowired
    private TShelvesMapper tShelvesMapper;
    @Autowired
    private CommodityMapper commodityMapper;
    @Autowired
    private WarehouseMapper warehouseMapper;


    @Override
    public PageEntity<TIoWarehouse> findPage(Integer current, Integer length, String idNo, String batch) {
        Integer count = ioWarehouseMapper.selectCount(idNo,batch);
        List<TIoWarehouse> ioWarehouses = ioWarehouseMapper.selectPage((current - 1) * length, length,idNo,batch);
        PageEntity<TIoWarehouse> tIoWarehousePageEntity = new PageEntity<>();
        tIoWarehousePageEntity.setData(ioWarehouses);
        tIoWarehousePageEntity.setCurrent(current);
        tIoWarehousePageEntity.setCount(count);
        return tIoWarehousePageEntity;
    }

    @Override
    @Transactional
    public void addInIoWarehouse(TIoWarehouse tIoWarehouse,String blankCargo) {
        //在转移的货位上添加数量
        Integer pNum = tIoWarehouse.getPNum();
        TCargo tCargo = cargoMapper.selectOne(new QueryWrapper<TCargo>().lambda().eq(TCargo::getCargoNo,blankCargo));
        tCargo.setProductNum(tCargo.getProductNum() + pNum);
        //更新
        cargoMapper.updateById(tCargo);
        TMenu tMenu = tMenuMapper.selectOne(new QueryWrapper<TMenu>().lambda().eq(TMenu::getWareNo, tIoWarehouse.getWName()));
        tIoWarehouse.setWName(tMenu.getWareName());
        LocalDateTime now = LocalDateTime.now();
        //设为当前中国时间
        Timestamp timestamp = Timestamp.valueOf(now);
        tIoWarehouse.setIoTime(timestamp);
        System.out.println(tIoWarehouse);
        ioWarehouseMapper.insert(tIoWarehouse);

        //添加详情表
        TShelves tShelves = (TShelves) tShelvesMapper.selectByShelvesNo(tCargo.getShelvesNo());
        TWarehouse tWarehouse = warehouseMapper.selectTWarehouseByGoodsNo(tShelves.getWarehouseId());
        TIomWarehouse tIomWarehouse = new TIomWarehouse();
        Commodity commodity = commodityMapper.selectOne(new QueryWrapper<Commodity>().lambda().eq(Commodity::getPName, tIoWarehouse.getPName()));
        tIomWarehouse.setIomGoodsno(String.valueOf(commodity.getCompanyId()));
        tIomWarehouse.setIomGoods(tIoWarehouse.getPName());
        tIomWarehouse.setIomName(tIoWarehouse.getWName());
        tIomWarehouse.setIomArea(tWarehouse.getGoodsName());
        tIomWarehouse.setIomShelves(tShelves.getShelvesNo());
        tIomWarehouse.setIomCargoloca(blankCargo);
        tIomWarehouse.setIomTime(now);
        tIomWarehouse.setIomNo(tIoWarehouse.getIdNo());
        tIomWarehouse.setIomFlag(1);
        tIomWarehouse.setIomNumber(tIoWarehouse.getPNum());
        System.out.println(tIomWarehouse);
        tIomWarehouseMapper.insert(tIomWarehouse);
    }

    @Override
    @Transactional
    public void addOutIoWarehouse(TIoWarehouse tIoWarehouse) {
        LocalDateTime now = LocalDateTime.now();
        //设为当前中国时间
        Timestamp timestamp = Timestamp.valueOf(now);
        tIoWarehouse.setIoTime(timestamp);
        System.out.println(tIoWarehouse);
        ioWarehouseMapper.insert(tIoWarehouse);
    }
}
